package com.club.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.entity.ActivitySignUp;
import com.club.mapper.ActivitySignUpMapper;
import com.club.service.ActivitySignUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.time.LocalDateTime;

/**
 * 活动报名服务实现类
 */
@Service
public class ActivitySignUpServiceImpl extends ServiceImpl<ActivitySignUpMapper, ActivitySignUp> implements ActivitySignUpService {

    @Autowired
    private ActivitySignUpMapper activitySignUpMapper;

    @Override
    public boolean signUp(ActivitySignUp activitySignUp) {
        // 检查是否已经报名
        if (isSignedUp(activitySignUp.getActivityId(), activitySignUp.getUserId())) {
            return false;
        }
        activitySignUp.setSignupTime(LocalDateTime.now());
        activitySignUp.setStatus(0); // 默认待审核状态
        return save(activitySignUp);
    }

    @Override
    public boolean cancelSignUp(Long activityId, Long userId) {
        ActivitySignUp signUp = getSignUpByActivityAndUser(activityId, userId);
        if (signUp != null) {
            return removeById(signUp.getId());
        }
        return false;
    }

    @Override
    public boolean auditSignUp(Long id, Integer status, String remark) {
        ActivitySignUp signUp = getById(id);
        if (signUp != null) {
            signUp.setStatus(status);
            signUp.setRemark(remark);
            return updateById(signUp);
        }
        return false;
    }

    @Override
    public List<ActivitySignUp> getSignUpsByActivityId(Long activityId) {
        return activitySignUpMapper.selectByActivityId(activityId);
    }

    @Override
    public List<ActivitySignUp> getSignUpsByUserId(Long userId) {
        return activitySignUpMapper.selectByUserId(userId);
    }

    @Override
    public ActivitySignUp getSignUpByActivityAndUser(Long activityId, Long userId) {
        return activitySignUpMapper.selectByActivityAndUser(activityId, userId);
    }

    @Override
    public boolean isSignedUp(Long activityId, Long userId) {
        ActivitySignUp signUp = getSignUpByActivityAndUser(activityId, userId);
        return signUp != null;
    }

    @Override
    public int getSignUpCount(Long activityId, Integer status) {
        return activitySignUpMapper.countByActivityIdAndStatus(activityId, status);
    }
}